package com.graduation.controller;

import com.graduation.config.ObjectConfig;
import com.graduation.entity.Cart;
import com.graduation.entity.User;
import com.graduation.service.CartService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class CartController {

    @Resource
    private CartService cartService;

    @RequestMapping(value = "/addCart.do")
    @ResponseBody
    public Map<String,String> intoCart(@RequestParam(name = "id")String id, @RequestParam("num")Integer num, HttpServletRequest request){
        Map<String,String> map = new HashMap<>();
        User user = (User) request.getSession(false).getAttribute("user");
        Cart cart = cartService.queryGoodsInfo(id,num,user.getEmail());
        if(cart != null){
            map.put("code","200");
            map.put("info","加入购物车成功！");
            return map;
        }
        map.put("code","400");
        map.put("info","加入购物车失败！");
        return map;
    }

    /**
     * 查看购物车
     * @param request
     * @return
     */
    @RequestMapping(value = "/queryGoodsToCart.do",method = RequestMethod.GET)
    public ModelAndView queryGoodsByEmail(HttpServletRequest request){
        User user = (User) request.getSession(false).getAttribute("user");
        List<Cart> carts = cartService.queryGoodsByEmail(user.getEmail());
        //拿到容器中的对象
        ApplicationContext ac = new AnnotationConfigApplicationContext(ObjectConfig.class);
        ModelAndView mv = (ModelAndView) ac.getBean("createMandV");
        mv.addObject("carts",carts);
        double totalPrice = 0.0;
        Integer totalNum  = 0;
        //拼接请求地址
        StringBuilder param = new StringBuilder();
        //算出总量和总价
        for (Cart c:carts) {
            Integer num = Integer.valueOf(c.getNum());
            totalNum += num;
            Double price = Double.valueOf(c.getPrice());
            totalPrice += (num * price);
            param.append("goodsId=" + c.getGoodsid() + "&") ;
        }
        mv.addObject("totalNum",totalNum);
        mv.addObject("params",param.toString());
        mv.addObject("totalPrice",totalPrice);
        mv.setViewName("cart");
        return mv;
    }
    /*删除*/
    @RequestMapping(value = "deleteGoods.do",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,String> deleteGoods(String id){
        Map<String,String> map =new HashMap<>();
        Integer res = cartService.deleteGoodsById(id);
        if(res == 1){
            map.put("code","200");
            map.put("msg","删除成功");
            return map;
        }
        map.put("code","404");
        map.put("msg","删除失败");
        return map;
    }
}
